﻿//Tag
package tagul {
    import flash.display.GraphicsPath;
    import flash.display.GraphicsSolidFill;
    import flash.display.GraphicsStroke;
    import flash.display.IGraphicsData;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.ColorTransform;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.net.URLRequest;
    import flash.net.navigateToURL;


    public class Tag extends Sprite {
        public function Tag(arg1:URLRequest, arg2:Rectangle, arg3:Matrix, arg4:GraphicsSolidFill, arg5:Object, arg6:Rectangle) {
            super();
            this._url = arg1;
            this._bbox = arg2;
            this._color = arg4;
            this._rollOverBox = new Shape();
            if (arg5.rollOverBoxStroke != null) {
                this._rollOverBox.graphics.lineStyle(arg5.rollOverBoxStroke.weight / this.getMatrixScale(arg3), arg5.rollOverBoxStroke.color, arg5.rollOverBoxStroke.alpha);
            }
            this._rollOverBox.graphics.beginFill(arg5.rollOverBoxColor.color, arg5.rollOverBoxColor.alpha);
            this._rollOverBox.graphics.drawRoundRect(this._bbox.x, this._bbox.y, this._bbox.width, this._bbox.height, this._bbox.width / 15);
            this._rollOverBox.graphics.endFill();
            this._rollOverBox.alpha = 0;
            addChild(this._rollOverBox);
            this._glyphs = new Sprite();
            addChild(this._glyphs);
            this._matrix = arg3;
            transform.matrix = arg3;
            this._frame = 0;
            this._frames = Math.max(Math.round(arg5.animationSpeed * 30), 1);
            this._rollOverBoxAlphaStep = 1 / this._frames;
            this._currentColorMatrix = { "r": 0, "g": 0, "b": 0, "a": 0 };
            var loc1:* = this.parseColor(this._color);
            var loc2:* = arg5.rollOverTextColor != null ? this.parseColor(arg5.rollOverTextColor) : loc1;
            this._rollOverTextColorStep = { "r": (loc2.r - loc1.r) / this._frames, "g": (loc2.g - loc1.g) / this._frames, "b": (loc2.b - loc1.b) / this._frames, "a": (loc2.a - loc1.a) / this._frames };
            var loc3:* = arg5.rollOverZoom ? 0.15 + this.getMatrixScale(this._matrix) / 1.15 : this.getMatrixScale(this._matrix);
            var loc4:* = arg5.rollOverRotate ? 0 : this.getMatrixAngle(this._matrix);
            var loc5:*;
            (loc5 = this._matrix.clone()).concat(this.getMatrix(loc3, loc4, 0, 0, 1));
            var loc6:* = loc5.transformPoint(new Point(this._bbox.x, this._bbox.y));
            var loc7:* = loc5.transformPoint(new Point(this._bbox.x + this._bbox.width, this._bbox.y + this._bbox.height));
            var loc8:* = 0;
            var loc9:* = 0;
            if (loc6.x < arg6.x) {
                loc8 = arg6.x - loc6.x;
            }
            if (loc7.x > arg6.x + arg6.width) {
                loc8 = -loc7.x + arg6.x + arg6.width;
            }
            if (loc6.y < arg6.y) {
                loc9 = arg6.y - loc6.y;
            }
            if (loc7.y > arg6.y + arg6.height) {
                loc9 = -loc7.y + arg6.y + arg6.height;
            }
            this._rollOverMatrixStep = this.getMatrix(loc3, loc4, loc8, loc9, this._frames);
            this._forwardDirection = false;
            this.invertAnimation();
            return;
        }

        private function getMatrix(arg1:Number, arg2:Number, arg3:Number, arg4:Number, arg5:Number):flash.geom.Matrix {
            var loc1:* = new Matrix();
            var loc2:* = this._matrix.transformPoint(new Point(this._bbox.x + this._bbox.width / 2, this._bbox.y + this._bbox.height / 2));
            loc1.translate(-loc2.x, -loc2.y);
            var loc3:* = Math.pow(arg1 / this.getMatrixScale(this._matrix), 1 / arg5);
            loc1.scale(loc3, loc3);
            loc1.rotate((arg2 - this.getMatrixAngle(this._matrix)) / arg5);
            loc1.translate(loc2.x + arg3 / arg5, loc2.y + arg4 / arg5);
            return loc1;
        }

        private function getMatrixScale(arg1:flash.geom.Matrix):Number {
            return Math.sqrt(arg1.a * arg1.a + arg1.b * arg1.b);
        }

        private function getMatrixAngle(arg1:flash.geom.Matrix):Number {
            var loc1:* = this.getMatrixScale(arg1);
            var loc2:* = arg1.a / loc1;
            var loc3:* = arg1.b / loc1;
            var loc4:* = Math.asin(loc3);
            if (loc2 < 0) {
                loc4 = -Math.PI - loc4;
            }
            return loc4;
        }

        private function parseColor(arg1:flash.display.GraphicsSolidFill):Object {
            return { "r": arg1.color >> 16 & 255, "g": arg1.color >> 8 & 255, "b": arg1.color & 255, "a": arg1.alpha * 255 };
        }

        public function startAnimation():void {
            parent.swapChildrenAt((parent.numChildren / 2 - 1), parent.getChildIndex(this));
            this.stopAnimation();
            return;
        }

        private function invertAnimation():void {
            this._frame = this._frames - this._frame;
            this._forwardDirection = !this._forwardDirection;
            this._rollOverBoxAlphaStep = -this._rollOverBoxAlphaStep;
            this._rollOverMatrixStep.invert();
            this._rollOverTextColorStep.r = -this._rollOverTextColorStep.r;
            this._rollOverTextColorStep.g = -this._rollOverTextColorStep.g;
            this._rollOverTextColorStep.b = -this._rollOverTextColorStep.b;
            this._rollOverTextColorStep.a = -this._rollOverTextColorStep.a;
            return;
        }

        public function stopAnimation():void {
            this._rollOverBox.removeEventListener(Event.ENTER_FRAME, this.animate);
            this.invertAnimation();
            this._rollOverBox.addEventListener(Event.ENTER_FRAME, this.animate);
            return;
        }

        private function animate(arg1:Event):void {
            var loc1:* = null;
            if (this._frame >= 0 && this._frame < this._frames) {
                var loc2:*;
                var loc3:* = ((loc2 = this)._frame + 1);
                loc2._frame = loc3;
                this._rollOverBox.alpha = this._rollOverBox.alpha + this._rollOverBoxAlphaStep;
                loc1 = transform.matrix.clone();
                loc1.concat(this._rollOverMatrixStep);
                transform.matrix = loc1;
                this._currentColorMatrix.r = this._currentColorMatrix.r + this._rollOverTextColorStep.r;
                this._currentColorMatrix.g = this._currentColorMatrix.g + this._rollOverTextColorStep.g;
                this._currentColorMatrix.b = this._currentColorMatrix.b + this._rollOverTextColorStep.b;
                this._currentColorMatrix.a = this._currentColorMatrix.a + this._rollOverTextColorStep.a;
                this._glyphs.transform.colorTransform = new ColorTransform(1, 1, 1, 1, this._currentColorMatrix.r, this._currentColorMatrix.g, this._currentColorMatrix.b, this._currentColorMatrix.a);
            } else {
                if (this._forwardDirection) {
                    this._rollOverBox.alpha = 0;
                } else {
                    this._rollOverBox.alpha = 1;
                }
                this._rollOverBox.removeEventListener(Event.ENTER_FRAME, this.animate);
            }
            return;
        }

        public function openURL(arg1:String = "_self"):void {
            navigateToURL(this._url, arg1);
            return;
        }

        public function addGlyph(arg1:GraphicsPath, arg2:Number, arg3:Number):void {
            var loc1:Shape = new Shape();
            var myDrawing:Vector.<IGraphicsData> = new Vector.<IGraphicsData>();
    		myDrawing.push(this._color, new GraphicsStroke(2), arg1);

            loc1.graphics.drawGraphicsData(myDrawing);
            loc1.x = arg2;
            loc1.y = arg3;
            this._glyphs.addChild(loc1);
        }

        public function get bbox():Rectangle {
            return this._bbox;
        }

        public function get url():String {
            return this._url.url;
        }

        private var _url:URLRequest;

        private var _color:GraphicsSolidFill;

        private var _glyphs:flash.display.Sprite;

        private var _bbox:Rectangle;

        private var _matrix:Matrix;

        private var _rollOverBox:flash.display.Shape;

        private var _rollOverBoxAlphaStep:Number;

        private var _currentColorMatrix:Object;

        private var _rollOverTextColorStep:Object;

        private var _rollOverMatrixStep:Matrix;

        private var _frame:Number;

        private var _frames:Number;

        private var _forwardDirection:Boolean;
    }
}


